package com.swz.dcrm.ui.car;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.swz.dcrm.adpter.FragmentViewPagerAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Input;
import com.swz.dcrm.model.Tab;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.CarManagementViewModel;
import com.swz.dcrm.ui.viewmodel.MainViewModel;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class CarResFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tab)
    SlidingTabLayout slidingTabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.et_search)
    EditText etSearch;
    private List<String> titles;
    private List<Fragment> fragments;

    @Inject
    CarManagementViewModel carManagementViewModel;
    @Inject
    MainViewModel mainViewModel;

    public static CarResFragment newInstance() {

        return new CarResFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        carManagementViewModel.getCarTabs().observe(this,observer );
    }

    Observer observer=new Observer<BaseResponse<List<Tab>>>() {
        @Override
        public void onChanged(BaseResponse<List<Tab>> listBaseResponse) {
            carManagementViewModel.getCarTabsResult().removeObserver(this);
            if(listBaseResponse.isSuccess()){
                List<Tab> carTabs=listBaseResponse.getData();
                Collections.reverse(carTabs);
                fragments=new ArrayList<>();
                titles=new ArrayList<>();
                for(Tab carTab:carTabs){
                    titles.add(carTab.getName()+"("+carTab.getTypeCount()+")");
                    fragments.add(CarResItemFragment.newInstance(carTab.getCode()));
                }
                viewPager.setAdapter(new FragmentViewPagerAdapter(getChildFragmentManager(),fragments, titles));
                slidingTabLayout.setViewPager(viewPager);
            }
        }
    };
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_car_res;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.car_res_title));

        etSearch.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                String searchTag = etSearch.getText().toString().trim();
                mainViewModel.getInput().setValue(new Input(etSearch,0,false));
                if (!TextUtils.isEmpty(searchTag)) {
                    carManagementViewModel.getCarNameInWareHouse().setValue(searchTag);
                }else {
                    carManagementViewModel.getCarNameInWareHouse().setValue(null);
                }
                return true;
            }
            return false;
        });
        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
}
